﻿@model EntitySearchModel

<div class="panel panel-default">
    <!-- Default panel contents -->
    <div class="panel-heading">
        <strong class="font-size20">Search @(Model.Metadata.Name)</strong>
    </div>
    <div class="panel-body">
        <form id="form" class="form-horizontal" role="form" action="@Url.Action("Index")" method="get">
            @for (int i = 0; i < Model.Properties.Length; i++)
            {
                <div class="form-group has-feedback">
                    <label class="col-sm-2 control-label">@Model.Properties[i].Name</label>
                    <div class="col-sm-8">
                        @{switch (Model.Properties[i].Type)
                        {
                            case System.ComponentModel.DataAnnotations.CustomDataType.Date:
                            case System.ComponentModel.DataAnnotations.CustomDataType.DateTime:
                                <div class="input-group col-md-7">
                                    <input type="date" class="form-control col-md-3" id="Search.@(Model.Properties[i].Property.Name).Start" name="Search.@(Model.Properties[i].Property.Name).Start" />
                                    <span class="input-group-addon">To</span>
                                    <input type="date" class="form-control col-md-3" id="Search.@(Model.Properties[i].Property.Name).End" name="Search.@(Model.Properties[i].Property.Name).End" />
                                </div>
                                <script type="text/javascript">
                                    $('#Search.@(Model.Properties[i].Property.Name).Start').datepicker();
                                    $('#Search.@(Model.Properties[i].Property.Name).End').datepicker();
                                </script>
                                break;
                            case System.ComponentModel.DataAnnotations.CustomDataType.Currency:
                            case System.ComponentModel.DataAnnotations.CustomDataType.Integer:
                            case System.ComponentModel.DataAnnotations.CustomDataType.Number:
                            <div class="input-group col-md-7">
                                <input type="number" class="form-control col-md-3" id="Search.@(Model.Properties[i].Property.Name).Start" name="Search.@(Model.Properties[i].Property.Name).Start" />
                                <span class="input-group-addon">To</span>
                                <input type="number" class="form-control col-md-3" id="Search.@(Model.Properties[i].Property.Name).End" name="Search.@(Model.Properties[i].Property.Name).End" />
                            </div>
                            <script type="text/javascript">
                                $('#Search.@(Model.Properties[i].Property.Name).Start').change(function () {
                                    var number = new Number($(this).val());
                                    if (isNaN(number)) {
                                        $(this).val(0);
                                        return;
                                    }
                                });
                                $('#Search.@(Model.Properties[i].Property.Name).End').change(function () {
                                    var number = new Number($(this).val());
                                    if (isNaN(number)) {
                                        $(this).val(0);
                                        return;
                                    }
                                });
                            </script>
                                break;
                            case System.ComponentModel.DataAnnotations.CustomDataType.Other:
                                if (Model.Properties[i].CustomType == "Enum")
                                {
                                    EntityExtension.EnumItem[] items;
                                    if (Model.Properties[i].Property.PropertyType.IsGenericType)
                                    {
                                        items = Html.EnumAnalyze(Model.Properties[i].Property.PropertyType.GetGenericArguments()[0]);
                                    }
                                    else
                                    {
                                        items = Html.EnumAnalyze(Model.Properties[i].Property.PropertyType);
                                    }
                                    <select name="Search.@Model.Properties[i].Property.Name" class="form-control">
                                        @if (Model.Properties[i].Property.PropertyType.IsGenericType)
                                        {
                                            <option value="" selected="selected"></option>
                                        }
                                        @for (int s = 0; s < items.Length; s++)
                                        {
                                            <option value="@items[s].Value">@items[s].Name</option>
                                        }
                                    </select>
                                }
                                else
                                {
                                    <input type="text" class="form-control" name="Search.@Model.Properties[i].Property.Name" />
                                }
                                break;
                            case System.ComponentModel.DataAnnotations.CustomDataType.Boolean:
                            <select class="form-control" name="Search.@Model.Properties[i].Property.Name">
                                <option value="" selected="selected"></option>
                                <option value="True">Yes</option>
                                <option value="False">No</option>
                            </select>
                                break;
                            case System.ComponentModel.DataAnnotations.CustomDataType.Sex:
                            <select class="form-control" name="Search.@Model.Properties[i].Property.Name">
                                <option value="" selected="selected"></option>
                                <option value="True">Male</option>
                                <option value="False">Female</option>
                            </select>
                                break;
                            default:
                            <input type="text" class="form-control" name="Search.@Model.Properties[i].Property.Name" />
                                break;
                        }
                        }
                    </div>
                </div>
            }
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <input type="submit" class="btn btn-primary" value="Search" />
                    <a href="@Url.Action("Index")" class="btn btn-default">Back</a>
                    <input type="hidden" name="search" value="True"/>
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    $('#form').submit(function () {
        $(this).find(':input').each(function (index, element) {
            if (this.value == '')
                this.disabled = true;
        });
    })
</script>